﻿#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    BaseEditor(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    table=TD_POSITION;
    model=new QSqlTableModel(this);
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    ui->tableView->setModel(model);
    ui->tableView->setEditTriggers(QTableView::DoubleClicked);

    listModel=new QStringListModel(this);
    ui->listView->setModel(listModel);
    ui->listView->setEditTriggers(QListView::NoEditTriggers);

    updateTopic();
    createMenu();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::keyPressEvent(QKeyEvent *event)
{
    if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_S)
        save();
    else if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_Z)
        revert();
    else
    {
        switch (event->key()) {
        case Qt::Key_F1:
            newItem();
            break;
        case Qt::Key_F4:
            updateTopic();
            break;
        case Qt::Key_Delete:
            removeItem();
            break;
        }
    }
}

void Widget::generateMenu()
{
    menu->addAction(QStringLiteral("新记录 (F1)"),this,&Widget::newItem);
    menu->addSeparator();
    menu->addAction(QStringLiteral("更新位置 (F4)"),this,&Widget::updateTopic);
    menu->addSeparator();
    menu->addAction(QStringLiteral("删除 (Delete)"),this,&Widget::removeItem);
    menu->addSeparator();
    menu->addAction(QStringLiteral("撤销 (Ctrl+Z)"),this,&Widget::revert);
    menu->addAction(QStringLiteral("保存 (Ctrl+S)"),this,&Widget::save);
}

void Widget::newItem()
{
    AddItem item(this,table);
    if(!selectTopic.isEmpty())
        item.setTopic(selectTopic);
    item.exec();
}

void Widget::removeItem()
{
    QModelIndex index=ui->tableView->currentIndex();
    if(index.isValid())
        model->removeRow(index.row());
}

void Widget::updateTopic()
{
    if(listModel->rowCount() != 0)
        listModel->setStringList(QStringList());

    QStringList items=sp_getTopic(table);
    if(!items.isEmpty())
        listModel->setStringList(items);
}

void Widget::on_listView_doubleClicked(const QModelIndex &index)
{
    QString topic=index.data().toString();
    selectTopic=topic;
    updateView();
}

void Widget::updateView()
{
    if(!selectTopic.isEmpty())
    {
        model->setTable(table);
        model->setFilter(QString("topic='%1'").arg(selectTopic));
        model->select();
        setHeaderData();
    }
}

void Widget::setHeaderData()
{
    ui->tableView->hideColumn(0);   //id
    ui->tableView->hideColumn(1);   //topic
    model->setHeaderData(2,Qt::Horizontal,QStringLiteral("产品名称"));    //label
    ui->tableView->setColumnWidth(2,200);
    model->setHeaderData(3,Qt::Horizontal,QStringLiteral("装箱数量"));    //detail
    model->setHeaderData(4,Qt::Horizontal,QStringLiteral("附加说明"));    //addition
}

void Widget::on_kw_returnPressed()
{
    QString kw=ui->kw->text().trimmed();
    if(kw.isEmpty())
        return;

    model->setTable(table);
    model->setFilter(QString("label like '%%1%'").arg(kw));
    model->select();

    ui->tableView->hideColumn(0);   //id
    model->setHeaderData(1,Qt::Horizontal,QStringLiteral("货架名称"));    //topic
    model->setHeaderData(2,Qt::Horizontal,QStringLiteral("产品名称"));    //label
    ui->tableView->setColumnWidth(2,200);
    model->setHeaderData(3,Qt::Horizontal,QStringLiteral("装箱数量"));    //detail
    model->setHeaderData(4,Qt::Horizontal,QStringLiteral("附加说明"));    //addition
}

void Widget::on_btn_clean_clicked()
{
    ui->kw->clear();
}
